<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>admin2.0</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
		<link rel="stylesheet" href="./css/font.css">
		<link rel="stylesheet" href="./css/xadmin.css">
		<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
		<script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
		<script type="text/javascript" src="./js/xadmin.js"></script>
	</head>

	<body class="layui-anim layui-anim-up">
		<div class="x-nav">
			<span class="layui-breadcrumb">
        <a href="">人员信息管理</a>
        <a><cite>日程安排</cite></a>
      </span>
			<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
				<i class="layui-icon" style="line-height:30px">ဂ</i></a>
		</div>
		<div class="x-body">
			<xblock class="demoTable">
				<button class="layui-btn layui-btn-danger" data-type="delAll"><i class="layui-icon"></i>批量删除</button>
				<button class="layui-btn" onclick="x_admin_show('添加值班','./duty-add.html',600,600)"><i class="layui-icon"></i>添加</button>
			</xblock>

			<!--数据表格-->
			<table class="layui-table" id="test" lay-filter="filter">
			</table>

		</div>

		<!--操作按钮-->
		<script type="text/html" id="barDemo">
			<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
		</script>

		<script>
			/**配置数据表格*/
			layui.use('table', function() {
				var table = layui.table;

				table.render({
					id: 'idTest',
					elem: '#test',
					url: '/duty/search',
					method: 'post', //默认get请求
					request: { //自定义分页请求参数的名称
						pageName: 'page', //页码的参数名称，默认：page
						limitName: 'rows' //每页数据量的参数名，默认：limit
					},
					response: { //自定义返回的数据格式
						statusName: 'status' //数据状态的字段名称，默认：code
							,
						statusCode: 200 //成功的状态码，默认：0
							,
						msgName: 'hint' //状态信息的字段名称，默认：msg
							,
						countName: 'total' //数据总数的字段名称，默认：count
							,
						dataName: 'rows' //数据列表的字段名称，默认：data
					},
					even: true, //隔行背景
					page: true, //分页
					cols: [
						[{
								type: 'checkbox'
							},
							{
								field: 'id',
								title: 'ID',
								sort: true,
								width: 100
							},
							{
								field: 'doctorname',
								title: '医生姓名',
								width: 100
							},
							{
								field: 'hospitalname',
								title: '医馆名称',
								width: 100
							},
							{
								field: 'dutyDate',
								title: '值班日期',
								width: 200
							},
							{
								field: 'dutyStartTime',
								title: '开始时间',
								width: 150
							},
							{
								field: 'dutyEndTime',
								title: '结束时间',
								width: 198
							},
							{
								field: 'op',
								title: '操作',
								toolbar: '#barDemo',
								width: 200
							}
						]
					],
					done: function(res, curr, count) {
						//数据表格加载完成时调用此函数
						//如果是异步请求数据方式，res即为你接口返回的信息。
						//设置全部数据到全局变量
						var table_data = res.rows;
					}
				});

				//监听表格复选框选择
				table.on('checkbox(filter)', function(obj) {
					//console.log(obj);
				});

				//监听操作工具条
				table.on('tool(filter)', function(obj) {
					var data = obj.data;
					if(obj.event === 'detail') {
						layer.msg('ID：' + data.id + ' 的查看操作');
					} else if(obj.event === 'del') {
						layer.confirm('确认要删除吗？', function(index) {
							//发异步删除数据
							$.ajax({
								url: "/duty/delete/" + data.id,
								type: "get",
								dataType: "json",
								success: function(data) {
									if(data.resultCode == 200) {
										layer.close(index);
										layer.msg("删除成功", {
											icon: 6
										});
										table.reload("idTest");
									} else {
										layer.msg("删除失败", {
											icon: 5
										});
									}
								}
							});
							layer.msg('已删除!', {
								icon: 1,
								time: 1000
							});
						});
					} else if(obj.event === 'edit') {
						x_admin_show('编辑', 'duty-edit.html', data.id, 600, 500);
					}
				});

				var $ = layui.$,
					active = {
						reload: function() {
							var searchName = $('#searchName').val();
							//执行重载
							table.reload('idTest', {
								page: {
									curr: 1 //重新从第 1 页开始
								},
								where: {
									'username': searchName
								}
							});
						},
						//复选框删除
						delAll: function() {
							//获取选中数据
							var checkStatus = table.checkStatus('idTest');
							var data = checkStatus.data;
							var ids = new Array();
							for(var i = 0; i < data.length; i++) {
								console.log(data[i].id);
								ids.push(data[i].id);
							}
							//异步请求
							layer.confirm('确定删除？', function(index) {
								$.ajax({
									url: "/duty/delete/" + ids.join(','),
									type: "get",
									dataType: "json",
									success: function(data) {
										if(data.resultCode == 200) {
											layer.close(index);
											layer.msg("删除成功", {
												icon: 6
											});
											table.reload("idTest");
										} else {
											layer.msg("删除失败", {
												icon: 5
											});
										}
									},
									error: function() {
										layer.msg("删除失败", {
											icon: 5
										});
										table.reload("idTest");
									}
								});
							});
						}
					};

				//激活按钮
				$('.demoTable .layui-btn').on('click', function() {
					var type = $(this).data('type');
					active[type] ? active[type].call(this) : '';
				});
				$('#searchBtn').on('click', function() {
					var type = $(this).data('type');
					active[type] ? active[type].call(this) : '';
				});
			});

			/**配置日期插件*/
			layui.use('laydate', function() {
				var laydate = layui.laydate;
				//执行一个laydate实例
				laydate.render({
					elem: '#start' //指定元素
				});
				//执行一个laydate实例
				laydate.render({
					elem: '#end' //指定元素
				});
			});
		</script>
	</body>

</html>